<?php
/**
 * 葫芦家公用方法
 * @author flaravel
 */
namespace App\Http\Controllers;


use App\Http\Requests\PhoneRequest;
use App\Models\Coupon;
use App\Models\Product;
use App\Models\ShareProduct;
use App\Service\CouponService;
use App\Service\LogisticService;
use App\Service\MiniProgramService;
use App\Service\ShareService;
use App\Service\Sms;
use Flaravel\Upload\Fupload;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class CommonController extends Controller
{

    public function upload() {
        if($file = request()->file('file')) {
            if ($file->isValid()) {
                $url = Fupload::upload($file,'after_sale');
                return $this->success(config('upload.ossUrl').Str::after($url,'com/'));
            }
        }
        return $this->failed('请上传文件');
    }

    /**
     * 发送短信验证码
     *
     * @param PhoneRequest $request
     * @param Sms $sms
     * @return mixed
     */
    public function verificationCode(PhoneRequest $request,Sms $sms)
    {
        $phone = ltrim(phone($request->input('phone'), 'CN', 'E164'), '+86');

        $res = $sms->sendCode($phone);
        if($res === true || is_numeric($res)){
            if(app()->environment() == 'dev')
            {
                return $this->success(['code' => $res, 'message' => '发送成功,请注意查收']);
            }
            return $this->success('发送成功,请注意查收');
        }
        return $this->failed($res);
    }

    /**
     * 分享二维码页面
å     */
    public function shareView() {
        $id = request('goods_id', 0);
        $nick_name = request('nick_name') ? urldecode(request('nick_name')) : '';
        $avatar = request('avatar') ? request('avatar') : '';
        $filename = request('filename');
        $goods = Product::find($id);
        return view('share.goods', compact('goods', 'avatar', 'nick_name', 'id', 'filename'));
    }

    /**
     * 分享商品图册
     *
     * @return mixed
     */
    public function shareImg()
    {
        $user = auth('api')->user();
        $goods_id = request()->goods_id;

        if (is_null($goods_id)) {
            return $this->failed('分享失败');
        }
        $product  =  Product::findOrFail($goods_id);
        // 生成小程序二维码
        $page = 'pages/detail/detail';
        // 生成二维码数据
        $data = http_build_query(['uid' => is_null($user) ? 0 : $user->id, 'gid' => $goods_id]);
        $filename = app(MiniProgramService::class)->getAccountCode($data, $page);
        if (isset($filename['errcode'])) {
            return $this->failed('分享失败');
        }
        $nick_name = is_null($user) ? '葫芦家商城' : $user->nickname;
        $avatar = is_null($user) ? 'https://img.hulujia119.com/product/931a9b76e9a84a128e44b947ea8bd05e.jpeg' : $user->avatar;
        // 记录分享商品信息
        if ($product->commission_ratio > 0 && $user) {
            // 发送优惠券
            $coupon = Coupon::findOrFail(14);
            app(CouponService::class)->giveShareCouponToUser($coupon, $user->id);
            ShareProduct::updateOrCreate([
                'member_id' => $user->id,
                'product_id' => $goods_id,
                'status' => 1
            ],[
                'member_id' => $user->id,
                'product_id' => $goods_id,
                'product_commission_ratio' => $product->commission_ratio,
                'status' => 1,
            ]);
        }
        // 生成分享海报
        return $this->success([
            'product_image' => $product->image,
            'codeLogo' => $filename,
            'headIcon' => $avatar,
            'username' => $nick_name
        ]);
    }

    /**
     * 获取物流公司编码
     * @return mixed
     */
    public function getLogistiCompany()
    {
        $res = app(LogisticService::class)->getLogisticCompany();
        return $this->success($res);
    }

    // 分享产品
    public function shareGoods()
    {
        $user = auth('api')->user();
        $product  =  Product::findOrFail(request()->input('goods_id'));
        // 发送优惠券
        if ($product->commission_ratio > 0 && $user) {
            $coupon = Coupon::findOrFail(14);
            app(CouponService::class)->giveShareCouponToUser($coupon, $user->id);
            ShareProduct::updateOrCreate([
                'member_id' => $user->id,
                'product_id' => request()->input('goods_id'),
                'status' => 1
            ],[
                'member_id' => $user->id,
                'product_id' => request()->input('goods_id'),
                'product_commission_ratio' => $product->commission_ratio,
                'status' => 1,
            ]);
        }
        return $this->message('分享成功');
    }
}
